package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.common.constant.RedisConst;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.dto.product.SkuSaleDto;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品搜索")
@RestController
@RequestMapping("/api/product")
@Slf4j
public class ProductController {

    @Autowired
    ProductService productService; //代理对象。 执行切面扩展。   事务    分布式锁    日志

    @Autowired
    RedissonClient redissonClient;

    @Operation(summary = "商品列表搜索")
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<ProductSku>> findPage(@Parameter(name = "page",description = "第几页",required = true) @PathVariable("page") Integer page,
                                                 @Parameter(name = "limit",description = "每页条数",required = true) @PathVariable("limit") Integer limit,
                                                 @Parameter(name = "productSkuDto",description = "查询条件",required = false) ProductSkuDto productSkuDto){
        PageInfo<ProductSku> pageInfo = productService.findPage(page,limit,productSkuDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "商品详情")
    @GetMapping("/item/{skuId}")
    public Result<ProductItemVo> item(@Parameter(name = "skuId",description = "sku的id",required = true) @PathVariable("skuId") Long skuId){


        //校验
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.PRODUCT_BLOOM_FILTER);
        if(!bloomFilter.contains(skuId)){ //判断商品在布隆过滤器中是否存在？
            ProductItemVo productItemVo = new ProductItemVo();
            log.info("from RBloomFilter data not exists 返回空对象= "+ productItemVo);
            return Result.build(productItemVo,ResultCodeEnum.SUCCESS);
        }


        ProductItemVo productItemVo = productService.item(skuId);
        return Result.build(productItemVo,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "获取商品sku信息")
    @GetMapping("/getBySkuId/{skuId}")
    public ProductSku getBySkuId(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable Long skuId) {
        ProductSku productSku = productService.getBySkuId(skuId);
        return productSku;
    }



    @Operation(summary = "更新商品sku销量")
    @PostMapping("/updateSkuSaleNum")
    public Boolean updateSkuSaleNum(@RequestBody List<SkuSaleDto> skuSaleDtoList) {
        return productService.updateSkuSaleNum(skuSaleDtoList);
    }
}
